<template>
	<el-container>
		<el-header style="height: auto;">
			<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="createRecord">招商政策</el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				<el-button type="success" plain icon="el-icon-download" :disabled="selection.length==0" @click="batch_download"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.policyName" placeholder="请输入政策名称"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" hidePagination stripe remoteSort remoteFilter>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="序号" type="index" width="80"></el-table-column>
				<el-table-column label="政策名称" prop="policyName" width="120" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="政策类型" prop="policyType" width="120">
					<template #default="scope">
						<el-tag type="primary" size="small" v-if="scope.row.policyType===1">人才落户</el-tag>
						<el-tag type="danger" size="small" v-else-if="scope.row.policyType===2">房租减税</el-tag>
						<el-tag type="danger" size="small" v-else-if="scope.row.policyType===3">装修补贴</el-tag>
						<el-tag type="danger" size="small" v-else-if="scope.row.policyType===4">税费减免</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="生效日期" prop="effectTime" width="180" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="政策状态" prop="policyStatus" width="120">
					<template #default="scope">
						<el-tag type="primary" size="small" v-if="scope.row.policyStatus===1">有效</el-tag>
						<el-tag type="danger" size="small" v-else-if="scope.row.policyStatus===2">失效</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="政策内容" prop="policyContent" width="180" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="附件" prop="attachId" width="180" :show-overflow-tooltip="true">
					<template #default="scope">
						<attach-menu :attachIds="scope.row.attachId" :roleFlag="1" :preview="true"></attach-menu>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="200">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
	<recordDetails v-if="dialog.detailsSave" ref="leaActDialog" @closed="dialog.detailsSave=false"></recordDetails>
</template>

<script>
import AttachMenu from '@/views/module/attachMenu'
import scSelectFilter from '@/components/scSelectFilter'
import saveDialog from './save'
import recordDetails from "./details";
export default {
	name: 'policy type',
	components: {
		saveDialog,
		scSelectFilter,
		AttachMenu,
		recordDetails
	},
	data() {
		return {
			dialog: {
				save: false,
				detailsSave:false
			},
			filterData: [
				{
					title: "政策类型",
					key: "policyType",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "人才落户",
							value: "1"
						},
						{
							label: "房租减税",
							value: "2"
						},
						{
							label: "装修补贴",
							value: "3"
						},
						{
							label: "税费减免",
							value: "4"
						}
					]
				},
				{
					title: "政策状态",
					key: "policyStatus",
					multiple: false,
					options: [{
						value: '',
						label: '全部'
					}, {
						value: '1',
						label: '有效'
					}, {
						value: '2',
						label: '失效'
					}],
				}
			],
			apiObj: this.$API.policy.policyType.getPolicyTypeList,
			search: {
				policyName:'',
				policyType:'',
				policyStatus:'',
				keyword: null
			},
			selection: [],
		}
	},
	mounted() {
	},
	methods: {
		//添加
		createRecord() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.detailsSave = true
			this.$nextTick(() => {
				this.$refs.leaActDialog.open().setData(row)
			})
		},
		//删除
		async table_del(row) {
			var res = await this.$API.policy.policyType.deletePolicyType.get({id: row.id});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async batch_del() {
			await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				var recordIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					recordIdArr.push(this.selection[i].id);
				}
				const loading = this.$loading();
				var res = await this.$API.policy.policyType.batchDeletePolicyType.get({ids: recordIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {
			})
		},
		async batch_download(){
			await this.$confirm(`确定导出选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				var recordIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					recordIdArr.push(this.selection[i].id);
				}
				this.$API.policy.policyType.getExpPolicyType.fileDownload({ids: recordIdArr});
			}).catch(() => {
			})
		},
		//搜索
		async upsearch() {
			this.$refs.table.upData(this.search)
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		filterChange(data) {
			this.search.policyType = data.policyType;
			this.search.policyStatus = data.policyStatus;
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSaveSuccess() {
			this.$refs.table.refresh()
		},
	}
}
</script>

<style>
</style>
